package com.code.app.floatview;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class FloatViewManager {

    private Context context;

    private WindowManager windowManager;

    private static FloatViewManager floatViewManager;

    private FloatCircleView floatCircleView;

    private WindowManager.LayoutParams params;

    private float startX;

    private float startY;

    private float x0;
    private float y0;

    private FloatViewManager(Context context) {
        this.context = context;
        //通过windowManager来操控浮窗的显示和隐藏以及位置的改变
        this.windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        this.floatCircleView = new FloatCircleView(context);
        this.floatCircleView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x0 = startX = event.getRawX();
                        startY = event.getRawY();
                        floatCircleView.setDrag(true);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float currentX = event.getRawX();
                        float currentY = event.getRawY();
                        params.x += currentX - startX;
                        params.y += currentY - startY;
                        windowManager.updateViewLayout(floatCircleView, params);
                        startX = currentX;
                        startY = currentY;
                        floatCircleView.setDrag(true);
                        break;
                    case MotionEvent.ACTION_UP:
                        float upX = event.getRawX();
                        if (upX > getScreenWidth() / 2) {
                            params.x = getScreenWidth() - floatCircleView.width;
                        } else {
                            params.x = 0;
                        }
                        windowManager.updateViewLayout(floatCircleView, params);
                        floatCircleView.setDrag(false);
                        //防止拖拽事件与点击事件冲突
                        if (Math.abs(upX - x0) > 6) {
                            return true;
                        } else {
                            return false;
                        }

                }
                return false;
            }
        });
    }

    private int getScreenWidth() {
        return windowManager.getDefaultDisplay().getWidth();
    }

    public static FloatViewManager getInstance(Context context) {
        if (floatViewManager == null) {
            synchronized (FloatViewManager.class) {
                if (floatViewManager == null) {
                    floatViewManager = new FloatViewManager(context);
                }
            }
        }
        return floatViewManager;
    }

    public void showCircleView() {
        params = new WindowManager.LayoutParams();
        params.width = floatCircleView.width;
        params.height = floatCircleView.height;
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 0;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        params.format = PixelFormat.RGBA_8888;
        windowManager.addView(floatCircleView, params);
    }

}
